<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <label for="copy">内容:</label>
  <input type="text" id="copy" value="这里写内容" />
  <button data-copytarget="#copy">复制</button>
  <script>
  (function () {

      'use strict';

      // 添加点击事件
      document.body.addEventListener('click', copy, true);

      // copy事件
      function copy(e) {

        // 找到要复制的区域
        var
          t = e.target,
          c = t.dataset.copytarget,
          inp = (c ? document.querySelector(c) : null);

        // 要复制的区域是否可以选中
        if (inp && inp.select) {

          // 选中
          inp.select();

          try {
            // 执行复制
            document.execCommand('copy');
            inp.blur();
          }
          catch (err) {
            alert('please press Ctrl/Cmd+C to copy');
          }

        }

      }

    })();
  </script>
</body>
</html>